<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav main-header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            发货拆箱
        </div>
    </header>
    <div class="aui-bar aui-bar-nav sub-header"></div>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;padding-bottom: 50px;">
        <div class="white_background">
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">包装袋码</div>
                        <input type="text" class="aui-input" placeholder="请扫描包装袋码" style="width: 80%;" id="boxnum"
                            onclick="getIdSelector();" onfocus="focusFn(this)">

                    </div>
                </li>
            </ul>
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">目标容器</div>
                        <input type="text" class="aui-input" placeholder="请扫描容器" style="width: 80%;"
                            id="trayyinfotargetid" onclick="getIdSelector();" onfocus="focusFn(this)">

                    </div>
                </li>
            </ul>


            <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()" id="submitButton">提交</div>

        </div>
    </div>

    <div>
        <div style="height:60px;"></div>
        <footer class="aui-bar aui-bar-tab" id="footer">
            <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
                <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
            </div>
        </footer>
    </div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">
    var scanRule = ""; //箱码解析规则
    var bindList = "";//物料数据列表

    //初始化查询仓库下拉框
    apiready = function () {

        $("#boxnum").focus();

        //查询箱码解析规则
        initScanRule();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //alert(ret);扫码的结果都是编码

            //2:
            if (idSelector == 'boxnum') {

                $("#boxnum").val(ret);
                //查询
                getBoxnum(ret);

                // 目标容器
            } else if (idSelector == 'trayyinfotargetid') {

                $("#trayyinfotargetid").val(ret);


            }
        });
    }

    //绑定
    function save() {
        if (!clickThrottle()) {
            return;
        }
        var trayyinfotargetid = $("#trayyinfotargetid").val();
        var boxnum = $("#boxnum").val();
        const submitButton = document.getElementById('submitButton');

        submitButton.style.pointerEvents = "none"
        //包装袋码
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            api.toast({
                msg: '请扫描包装袋码！',
                duration: 3000
            });
            submitButton.style.pointerEvents = "auto"
            return;
        }
        //目标容器
        if (trayyinfotargetid == "" || trayyinfotargetid == null || trayyinfotargetid == undefined) {
            api.toast({
                msg: '请扫描目标容器！',
                duration: 3000
            });
            submitButton.style.pointerEvents = "auto"
            return;
        }

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/shipmentUnboxing";
        var data = {
            values: {
                boxnum: boxnum, //原容器条码
                ctBarcodeNew: trayyinfotargetid, //目标容器条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    api.hideProgress();
                    submitButton.style.pointerEvents = "auto"
                    api.toast({
                        msg: "提交成功",
                        duration: 3000
                    });
                    //光标回到目标容器条码
                    $("#trayyinfotargetid").focus();
                    $("#trayyinfotargetid").select();
                    idSelector = "trayyinfotargetid";
                } else {
                    api.hideProgress();
                    submitButton.style.pointerEvents = "auto"
                    $("#trayyinfotargetid").val("");
                    $("#trayyinfotargetid").select();
                    idSelector = "trayyinfotargetid";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
                submitButton.style.pointerEvents = "auto"
                $("#trayyinfotargetid").val("");
                $("#trayyinfotargetid").select();
                idSelector = "trayyinfotargetid";
                api.hideProgress();

            }
        });
    }

    //包装袋码监听回车事件
    $("#boxnum").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                console.log(tm);
                $("#boxnum").val(tm);
                $("#boxnum").focus();
                $("#boxnum").select();
                getBoxnum();
            }

        }
    });
    //目标容器监听回车事件
    $("#trayyinfotargetid").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                console.log(tm);
                $("#trayyinfotargetid").val(tm);
                $("#trayyinfotargetid").focus();
                $("#trayyinfotargetid").select();
            }

        }
    });

    //扫码查询返回物料信息
    function getBoxnum() {
        var boxnum = $("#boxnum").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchShipMrl";
        var data = {
            values: {
                boxnum: boxnum,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                console.log(111)
                //返回成功
                if (myobj.code == "0") {
                    console.log(321)
                    //光标回到目标容器条码
                    $("#trayyinfotargetid").focus();
                    $("#trayyinfotargetid").select();
                    idSelector = "trayyinfotargetid";
                    //失败
                } else if (myobj.code == "1") {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }

    function focusFn(ele) {
        $api.attr(ele, 'readonly', 'readonly');
        setTimeout(() => {
            $api.removeAttr(ele, 'readonly');
        }, 200);
    };
</script>

</html>